using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace DSEngine.Common
{
    public class ActerManager
    {
        public bool IsPause { get; set; }
        private static ActerManager instace;
        public static ActerManager Instance
        {
            get
            {
                if (instace == null)
                    instace = new ActerManager();
                return instace;
            }
        }
        List<Acter> acterList = new List<Acter>();
        public void RemoveActer(Acter acter)
        {
            acterList.Remove(acter);
        }

        public void AddActer(Acter acter)
        {
            acterList.Add(acter);
        }

        public void Pause()
        {
            this.IsPause = true;
        }
        public void Resume()
        {
            this.IsPause = false;
        }

        internal void Update(GameTime gameTime)
        {
            if (IsPause)
                return;
            List<Acter> acter2Remove = new List<Acter>();
            foreach (var item in acterList)
            {
                if (item.IsPause)
                    continue;
                item.CurrentDuring += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                if (item.CurrentDuring > item.During)
                {

                    item.CurrentDuring = 0;
                    switch (item.RepeatType)
                    {
                        case Acter.ActerRepeatType.Restart:
                            {
                                item.CurrentValue1 = item.OriValue1;
                                item.CurrentValue2 = item.OriValue2;
                                item.OnEnd();
                            }
                            break;
                        case Acter.ActerRepeatType.Reverse:
                            {
                                item.Step1 *= -1;
                                item.Step2 *= -1;
                                item.OnEnd();
                            }
                            break;
                        case Acter.ActerRepeatType.Forever:
                            {
                                item.OriValue1 = item.CurrentValue1;
                                item.OriValue2 = item.CurrentValue2;
                            }
                            break;
                        case Acter.ActerRepeatType.None:
                            {
                                item.OnEnd();
                                acter2Remove.Add(item);
                            }
                            break;
                    }
                   
                }
                if (item.Step2 != 0)
                {
                    Vector2 v = new Vector2();
                    v.X = (float)(item.Step1 * gameTime.ElapsedGameTime.TotalMilliseconds);
                    v.Y = (float)(item.Step2 * gameTime.ElapsedGameTime.TotalMilliseconds);
                    item.CurrentValue1 += v.X;
                    item.CurrentValue2 += v.Y;
                    v.X = item.CurrentValue1;
                    v.Y = item.CurrentValue2;
                    item.ValueType.SetValue(item.Target, v, null);
                }
                else
                {
                    float f = (float)(item.Step1 * gameTime.ElapsedGameTime.TotalMilliseconds);
                    item.CurrentValue1 += f;
                    item.ValueType.SetValue(item.Target, item.CurrentValue1, null);
                }
                
            }
            foreach (var item in acter2Remove)
            {
                acterList.Remove(item);
            }
        }
    }
}
